package coop.bancocredicoop.proyectos.gd;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServletUtils {

	private static final Log log = LogFactory.getLog(ServletUtils.class);
	
	public static boolean isParameterEmpty(HttpServletRequest request, String paramName) {
		String parameterValue = request.getParameter(paramName);
		return (parameterValue == null ||
			    parameterValue.trim().equals(""));
	}
	
	public static String getParameterAsString(HttpServletRequest request, String paramName) {
		return getParameterAsString(request, paramName, null);
	}
	
	public static String getParameterAsString(HttpServletRequest request, String paramName, String defaultValue) {
		String parameterValue = request.getParameter(paramName);
		if (parameterValue == null || parameterValue.trim().equals("")) {
			return defaultValue;
		} else {
			return parameterValue;
		}
	}
	
	public static boolean getParameterAsBoolean(HttpServletRequest request, String paramName) {
		if (request.getParameter(paramName) == null) {
			return false;
		} else {
			return request.getParameter(paramName).toLowerCase().equals("true");
		}
	}

	public static boolean getParameterAsBoolean(HttpServletRequest request, String paramName, boolean defaultValue) {
		if (request.getParameter(paramName) == null) {
			return defaultValue;
		} else {
			return request.getParameter(paramName).toLowerCase().equals("true");
		}
	}
	
	public static Long getParameterAsLong(HttpServletRequest request, String paramName, Long defaultValue) {
		if (request.getParameter(paramName) == null) {
			return defaultValue;
		} else {			
			try {
				return Long.parseLong(request.getParameter(paramName));
			} catch (NumberFormatException e) {
				log.debug(String.format("%s no es un Long válido: %s", paramName, request.getParameter("customerId")));
				return defaultValue;
			}
		}
	}
	
	public static Integer getParameterAsInteger(HttpServletRequest request, String paramName) {
		if (request.getParameter(paramName) == null) {
			return 0;
		} else {			
			try {
				return Integer.parseInt(request.getParameter(paramName));
			} catch (NumberFormatException e) {
				log.debug(String.format("%s no es Integer válido: %s", paramName, request.getParameter("customerId")));
				return 0;
			}
		}
	}
	
	public static Date getParameterAsDate(HttpServletRequest request, String paramName, Date defaultDate) {
		
		String paramValue = request.getParameter(paramName);
		if (paramValue == null) {
			log.debug(String.format("Parameter '%s' not found, returning default %s", paramName, defaultDate));
			return defaultDate;
		} else {
			try {
				return DateParser.getDateFromString(paramValue);
			} catch (ParseException e) {
				log.error(String.format("Parameter '%s' is not valid (%s), returning default %s", paramName, paramValue, defaultDate));
				return defaultDate;
			}
		}
	}
	
	public static Map<String, Integer> getMapFromDate(Date date) {
		if (date != null) {
			Map<String, Integer> map = new HashMap<String, Integer>();
			Calendar cal = new GregorianCalendar();
			cal.setTime(date);
			map.put("year", cal.get(Calendar.YEAR));
			map.put("month", cal.get(Calendar.MONTH));
			map.put("day", cal.get(Calendar.DAY_OF_MONTH));
			return map; 
		} else {
			return null;
		}
	}
	
}
